<script setup>
import { ref, onMounted } from 'vue'
import FromDrawer from '@/components/from-drawer.vue'
import { toast } from '@/utils/elnotification'
import {
    getManagerList,
    updateStatusApi,
    updateManagerInfoApi,
    deleteManagerByIdApi,
    getuserLevelApi
} from '@/api/user'
import ChooseImage from '@/components/choose-image.vue'
import ListHeader from '@/components/list-header.vue'

//绑定表格数据
const tableData = ref([])
//总页数
const total = ref(0)
//当前页
const currentPage = ref(1)
//加载动画
const loading = ref(false)
//公告标题
const title1 = ref('')
//搜索表单数据
const searchform = ref({
    keyword: '',
    starttime: '',
    endtime: ''
})

//管理员列表
const roles = ref([])

//会员等级
const catagoryList = ref([])

const getuserLevel = async () => {
    const res = await getuserLevelApi()
    catagoryList.value = res.list
}

//获取表格数据
const getData = async (p = null) => {
    if (typeof p === 'number') {
        currentPage.value = p
    }
    loading.value = true
    try {
        const res = await getManagerList(currentPage.value, {
            keyword: searchform.value.keyword,
            user_level_id: searchform.value.user_level_id
        })

        tableData.value = res.list.map((item) => {
            item.statusLoading = false
            return item
        })
        total.value = res.totalCount
        roles.value = res.roles
    } catch (error) {
    } finally {
        loading.value = false
    }
}

//删除公告
const onDelete = async (row) => {
    fromDrawerRef.value.showLoading()
    try {
        await deleteManagerByIdApi(row.id)
        toast('删除成功')
        getData(1)
    } catch (error) {
        toast('当前是演示环境', 'error')
    } finally {
        fromDrawerRef.value.hideLoading()
    }
    console.log(row)
}

//新增管理员
const fromDrawerRef = ref(null)
const onAdd = () => {
    title1.value = '新增'
    fromDrawerRef.value.open()
    form.value = {
        username: '',
        password: '',
        nickname: '',
        status: 1,
        avatar: '',
        phone: '',
        email: '',
        user_level_id: 6
    }
}

//表单部分
const formRef = ref(null)
const form = ref({
    username: '',
    password: '',
    nickname: '',
    status: 1,
    avatar: '',
    phone: '',
    email: '',
    user_level_id: 6
})

//确认提交
const onSubmit = async () => {
    const val = await formRef.value.validate((val) => val)
    if (!val) return
    fromDrawerRef.value.showLoading()
    if (changId.value !== 0) {
        try {
            await updateManagerInfoApi(changId.value, { ...form.value })
            toast('编辑成功')
            getData(1)
        } catch (error) {
            toast('当前是演示环境', 'error')
        } finally {
            fromDrawerRef.value.hideLoading()
        }
    } else {
        try {
            await createManagerApi({ ...form.value })
            toast('新增成功')
            getData(1)
        } catch (error) {
            toast('当前是演示环境', 'error')
        } finally {
            fromDrawerRef.value.hideLoading()
        }
    }
}

//修改管理员信息
const changId = ref(0)
const onChange = (row) => {
    title1.value = '修改'
    const { username, password, nickname, status, avatar, phone, email, user_level_id, id } = row
    form.value = {
        username,
        password,
        nickname,
        status,
        avatar,
        phone,
        email,
        user_level_id
    }
    changId.value = id
    fromDrawerRef.value.open()
}

//重置
const onReset = () => {
    searchform.value.keyword = ''
    searchform.value.user_level_id = null
    getData()
}

//搜索
const onSearch = () => {
    getData()
}
//商品分类显示条件
const isShowCategorySelect = ref(false)

//修改状态
const onChangeStatus = async (status, row) => {
    row.statusLoading = true
    try {
        await updateStatusApi(row.id, status)
        toast('修改状态成功')
        row.status = status
    } catch (error) {
        toast('当前是演示环境', 'error')
    } finally {
        row.statusLoading = false
    }
}

onMounted(() => {
    getData()
    getuserLevel()
})
</script>

<template>
    <div>
        <el-row :gutter="20">
            <el-col :span="6">
                <el-card shadow="never" class="border-0">
                    <div class="flex text-sm items-center jc">
                        <el-icon :size="20" class="h-[40px] w-[40px] rounded-full bg-cyan-400 text-light-50">
                            <User />
                        </el-icon>
                        <div class="ml-2">
                            <h6 class="font-bold  mb-1">16</h6>
                            <p class="text-gray-400 text-xs">分销员人数(人)</p>
                        </div>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="6">
                <el-card shadow="never" class="border-0">
                    <div class="flex text-sm items-center jc">
                        <el-icon :size="20" class="h-[40px] w-[40px] rounded-full bg-orange-400 text-light-50">
                            <ShoppingCart />
                        </el-icon>
                        <div class="ml-2">
                            <h6 class="font-bold  mb-1">2</h6>
                            <p class="text-gray-400 text-xs">订单数(人)</p>
                        </div>
                    </div>
                </el-card>
            </el-col>

            <el-col :span="6">
                <el-card shadow="never" class="border-0">
                    <div class="flex text-sm items-center ">
                        <el-icon :size="20" class="h-[40px] w-[40px] rounded-full bg-green-400  text-light-50">
                            <PriceTag />
                        </el-icon>
                        <div class="ml-2">
                            <h6 class="font-bold  mb-1">23</h6>
                            <p class="text-gray-400 text-xs">订单金额(元)</p>
                        </div>
                    </div>
                </el-card>
            </el-col>

            <el-col :span="6">
                <el-card shadow="never" class="border-0">
                    <div class="flex text-sm items-center jc">
                        <el-icon :size="20" class="h-[40px] w-[40px] rounded-full bg-indigo-400 text-light-50">
                            <Timer />
                        </el-icon>
                        <div class="ml-2">
                            <h6 class="font-bold  mb-1">5</h6>
                            <p class="text-gray-400 text-xs">提现次数(次)</p>
                        </div>
                    </div>
                </el-card>
            </el-col>

        </el-row>
        <el-card shadow="never" class="border-0 mt-5" v-loading="loading">
            <!-- 搜索 -->
            <el-form :model="searchform" label-width="80px" class="mb-3" size="small">
                <el-row :gutter="20">
                    <el-col :span="8" :offset="0">
                        <el-form-item label="关键词">
                            <el-input v-model="searchform.keyword" placeholder="请输入关键词" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item label="开始时间">
                            <el-date-picker v-model="searchform.starttime" type="date" placeholder="请选择开始时间"
                                value-format="YYYY-MM-DD" />
                        </el-form-item>
                    </el-col>
                    <el-col :span="8" :offset="0">
                        <el-form-item label="结束时间">
                            <el-date-picker value-format="YYYY-MM-DD" v-model="searchform.endtime" type="date"
                                placeholder="请选择结束时间" />
                        </el-form-item>
                    </el-col>



                </el-row>
                <div class="flex items-center justify-end mr-15">
                    <el-button type="primary" @click="onSearch">搜索</el-button>
                    <el-button @click="onReset">重置</el-button>

                </div>
            </el-form>
            <ListHeader @create="onAdd" @refresh="getData"></ListHeader>
        </el-card>
        <el-table :data="tableData" stripe style="width: 100%">
            <el-table-column label="管理员" width="200">
                <template #default="{ row }">
                    <div class="flex items-center">
                        <el-avatar :size="40" :src="row.avatar">
                            <img src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png" />
                        </el-avatar>
                        <div class="ml-3">
                            <h6>{{ row.username }}</h6>
                            <small>ID:{{ row.id }}</small>
                        </div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="user_level.name" label="会员等级" align="center" />
            <el-table-column label="登录注册" align="center">
                <template #default="{ row }"> 注册时间 : {{ row.create_time }} </template>
            </el-table-column>

            <el-table-column prop="content" label="状态" width="120">
                <template #default="{ row }">
                    <el-switch v-model="row.status" :active-value="1" :inactive-value="0"
                        @change="onChangeStatus($event, row)" :loading="row.statusLoading" :disabled="row.super == 1">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="180" align="center">
                <template #default="scoped">
                    <small v-if="scoped.row.super == 1" class="text-sm text-gray-500">暂无操作</small>
                    <div v-else>
                        <el-button type="primary" size="small" text @click="onChange(scoped.row)">修改</el-button>
                        <el-popconfirm title="是否要删除该管理员？" confirm-button-text="确认" cancel-button-text="取消"
                            @confirm="onDelete(scoped.row)">
                            <template #reference>
                                <el-button type="primary" size="small" text>删除</el-button>
                            </template>
                        </el-popconfirm>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <div class="flex justify-center justify-center mt-5">
            <el-pagination @current-change="getData" :current-page="currentPage" background layout="prev,pager,next"
                :total="total" />
        </div>
        <FromDrawer ref="fromDrawerRef" :title="title1" @submit="onSubmit">
            <el-form :model="form" ref="formRef" label-width="80px" :inline="false">
                <el-form-item label="用户名">
                    <el-input v-model="form.username" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item label="昵称">
                    <el-input v-model="form.nickname" placeholder="请输入昵称"></el-input>
                </el-form-item>
                <el-form-item label="头像">
                    <ChooseImage v-model:url="form.avatar"></ChooseImage>
                </el-form-item>
                <el-form-item label="会员等级">
                    <el-select v-model="form.user_level_id" placeholder="选中所属角色">
                        <el-option v-for="item in catagoryList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="手机">
                    <el-input v-model="form.phone" placeholder="请输入手机号码"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="form.email" placeholder="请输入邮箱号码"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="form.status" :active-value="1" :inactive-value="0"> </el-switch>
                </el-form-item>
            </el-form>
        </FromDrawer>
    </div>
</template>